package de.hsowl.tod4a.global.http;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Date;

import de.hsowl.tod4a.global.util.ZIPUnit;

/**
 * Represents a HTTP respone
 * 
 * @author Sebastian Witt
 * 
 */
public class HttpResponse {

	private byte[] header;

	public static String STATUS_OK = "HTTP/1.1 200 OK";
	public static String STATUS_BAD = "HTTP/1.1 404 Not Found";

	public String STATUS;
	public String DATE = "Date: " + (new Date()).toGMTString();
	public String SERVER = "Server: Java";
	public String VARY = "Vary: Accept-Encoding,User-Agent";
	public String ENCODING = "Content-Encoding: gzip";
	public String LENGTH = "Content-Length: ";
	public String CONNECTION = "Connection: keep-alive";
	public String TYPE = "Content-Type: text/html; charset=utf-8";
	public String CONTENT;

	private static String linebreak = "\r\n";

	public HttpResponse() {
	}

	public byte[] setContent(String msg, String status) {
		byte[] content = ZIPUnit.zip(msg.getBytes());
		String CONTENT_LENGTH_ = LENGTH + content.length;
		STATUS = status;

		String h = STATUS + linebreak + DATE + linebreak + SERVER + linebreak + VARY + linebreak + ENCODING + linebreak
				+ CONTENT_LENGTH_ + linebreak + CONNECTION + linebreak + TYPE + linebreak + linebreak;
		byte[] head = h.getBytes();
		header = new byte[content.length + head.length];
		System.arraycopy(head, 0, header, 0, head.length);
		System.arraycopy(content, 0, header, head.length, content.length);

		return header;
	}

	public void send(Socket socket) throws IOException {
		if (header != null) {
			socket.getOutputStream().write(header);
			socket.getOutputStream().flush();
		} else {
			throw new NullPointerException("Header is null");
		}
	}

	/**
	 * Reads an InputStream to generate a httpRespone object
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static synchronized HttpResponse getResponse(InputStream is) throws IOException {

		BufferedInputStream bis = new BufferedInputStream(is);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		StringBuffer header = new StringBuffer();

		HttpResponse resp = new HttpResponse();

		byte[] b = new byte[4096];
		bis.read(b);
		baos.write(b, 0, b.length);
		header.append(new String(b));

		while (bis.available() > 0) {
			bis.read(b);
			baos.write(b, 0, b.length);
			header.append(new String(b));
		}

		int length = Integer.parseInt(header.toString().split(resp.LENGTH)[1].split(linebreak)[0].trim());
		String status = header.toString().split(linebreak)[0];

		String split = linebreak + linebreak;
		byte[] buffer = new byte[length];
		System.arraycopy(baos.toByteArray(), header.toString().split(split)[0].length() + split.length(), buffer, 0, length);

		String content = new String(ZIPUnit.unzip(buffer));

		resp.setCONTENT(content);

		resp.STATUS = status;
		return resp;
	}

	public String getSTATUS() {
		return STATUS;
	}

	public void setSTATUS(String sTATUS) {
		STATUS = sTATUS;
	}

	public String getCONTENT() {
		return CONTENT;
	}

	public void setCONTENT(String cONTENT) {
		CONTENT = cONTENT;
	}
}
